﻿package dt.inlee.system.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dt.inlee.common.pagination.OrderBean;
import dt.inlee.common.pagination.PageBean;
import dt.inlee.common.pagination.SearchBean;
import dt.inlee.system.dao.api.ISubcompanyDao;
import dt.inlee.system.model.SubcompanyEntity;
//import java.util.ArrayList;
//import java.util.Date;

/**
 * @功能说明：集团业务层
 * @作者：LD
 * @时间：2013-8-12@
 */
@Service("subcompanyService")
public class SubcompanyService {
	@Resource(name = "subcompanyDao")
	private ISubcompanyDao subcompanyDao;

	public List<SubcompanyEntity> getList() {
		SearchBean sb = new SearchBean();
		return this.subcompanyDao.find(sb);
	}

	@SuppressWarnings("unused")
    private void checkEntity(SubcompanyEntity entity) {
		
	}

	public void setSubcompanyDao(ISubcompanyDao subcompanyDao) {
		this.subcompanyDao = subcompanyDao;
	}

	/**
	 * Add a data
	 * 
	 * @param
	 * @return
	 */
	@Transactional
	public int save(SubcompanyEntity entity) {
		// checkEntity(entity);
		return subcompanyDao.save(entity);
	}

	/**
	 * Update a data
	 * 
	 * @param
	 * @return
	 */
	@Transactional
	public void update(SubcompanyEntity entity) {
		// checkEntity(entity);
		subcompanyDao.update(entity);
	}

	/**
	 * Show a data
	 * 
	 * @param id
	 * @return
	 */
	public SubcompanyEntity getSubcompanyById(Integer id) {
		return subcompanyDao.get(id);
	}

	/**
	 * Delete an object by id
	 * 
	 * @param SubcompanyEntity
	 * @return
	 */
	@Transactional
	public void deleteById(int id) {
		subcompanyDao.delete(id);
	}

	public PageBean GetPage(SearchBean sb, int pageIndex, int pageSize) {

		PageBean pageBean = new PageBean();
		pageBean.setPageIndex(pageIndex);
		pageBean.setPageSize(pageSize);

		if (sb == null) {
			sb = new SearchBean();
		}
		if (sb.getOrders() == null) {
			OrderBean orderBean = new OrderBean("id", false);
			sb.addOrder(orderBean);

		}

		pageBean.setParams(sb);
		this.subcompanyDao.searchByPagination(pageBean);
//		total[0] = pageBean.getTotalData();
		return pageBean;
	}

	public List<SubcompanyEntity> getList(SearchBean sb) {
		return this.subcompanyDao.find(sb);
	}
	public PageBean getLists(SearchBean sb,int rows,int page)
	{
		return this.GetPage(sb, page, rows);
	}

	public SubcompanyEntity[] importsList(String strTemplateID, String filePath) {

		return null;
	}

	public int exportsList(String strTemplateID, String path, File file,
			String[] fields, SearchBean sb) {

		
		return 0;

	}

	public String[] getDefTitles() {
		return DefTitles;
	}

	public static String[] DefFields = new String[] { "subcompanyId",
			"keyName", "companyName", "bossId", "subcompanyCode",
			"customerManager", "industry", "keyvalue", "manager", "headPhone",
			"serviceType", "state", "createTime", "syncMark", "syncOperation",
			"syncStates", "syncTime" };
	public static String[] DefTitles = new String[] { "企业ID", "企业名称", "企业简称",
			"BOSS ID", "集团客户简码", "客户经理", "行业", "密钥值", "集团负责人", "负责人电话", "合作类型",
			"状态", "添加日期", "同步标志", "同步操作", "同步状态", "同步时间" };

	public static String[] ItemsServiceType = { "云共享模式", "混合云模式", "私有模式" };
	public static String[] ItemsState = { "正常", "暂停", "注销" };

	// 所有的字段保存名称
	@SuppressWarnings("rawtypes")
    private Map getMap() {
		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < DefFields.length; i++) {
			map.put(DefFields[i], DefTitles[i]);
		}
		return map;
	}

	@SuppressWarnings("unused")
    private int getIndexOfTitle(String title) {
		for (int i = 0; i < DefTitles.length; i++) {
			if (DefTitles[i].equals(title)) {
				return i;
			}
		}
		return -1;
	}

	@SuppressWarnings({"rawtypes", "unchecked"})
    public int writeExcel(File file, String[] fields, List<Map> entityList) {

		Map<String, String> map = getMap(); // 所有的字段
		OutputStream os = null;
		try {
			file.createNewFile();
			os = new FileOutputStream(file);

			WritableWorkbook wwb = null;

			try {
				wwb = Workbook.createWorkbook(os);

				WritableSheet ws = wwb.createSheet("Subcompany", 0);
				Label label = null;
				WritableFont titleFont = new WritableFont(WritableFont.TAHOMA,
						10, WritableFont.BOLD, false);
				WritableFont dataFont = new WritableFont(WritableFont.TAHOMA,
						10, WritableFont.NO_BOLD, false);
				WritableCellFormat titleFormat = new WritableCellFormat(
						titleFont);
				WritableCellFormat dataFormat = new WritableCellFormat(dataFont);

				for (int fieldIndex = 0; fieldIndex < fields.length; fieldIndex++) {
					// 填写标题
					String title = "";
					if (fields[fieldIndex].contains("=")) {
						title = fields[fieldIndex].substring(fields[fieldIndex]
								.indexOf("=") + 1);
						fields[fieldIndex] = fields[fieldIndex].substring(0,
								fields[fieldIndex].indexOf("="));
					} else {
						title = map.get(fields[fieldIndex]);
					}
					if (map.containsKey(fields[fieldIndex])) {
						label = new Label(fieldIndex, 0, title, titleFormat);
						ws.addCell(label);
					}

				}

				// 填写内容
				for (int row = 1; row <= entityList.size(); row++) {
					Map<String, String> mapValue = entityList.get(row - 1); // 获得一条数据的集合
					for (int column = 0; column < fields.length; column++) {
						if (mapValue.containsKey(fields[column])) {
							label = new Label(column, row, mapValue
									.get(fields[column]), dataFormat);
							ws.addCell(label);
						}
					}
				}

				wwb.write();
			} catch (Exception ex) {
			} finally {
				wwb.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (os != null) {
					os.close();
				}
			} catch (Exception ex) {

			}
		}
		return fields.length;
	}
	
}
